const express = require('express');

const query = require('./mysql'); //自定义一个模块
// console.log(query);

const bodyParser = require('body-parser')
// 提取body请求体数据用到的中间件,需要配置参数才能使用
var urlencodeParser = bodyParser.urlencoded({
    extended: false
});
const Router = express.Router(); //路由设置  Router==app

const { create, verify } = require('./token');
/*
    用户管理：
        * 验证用户名是否存在 完成
        * 注册 完成
        * 登陆
        * 修改密码
        * 删除xx用户
        * 清空用户
        * 数据渲染 分页 完成
*/



// 验证用户是否存在
Router.get('/checkname', async (req, res) => {
    //接受前端数据
    let {
        username
    } = req.query;
    // console.log(username);
    //做数据库查询
    if (username) {
        let sql = `SELECT * FROM userlist WHERE username='${username}'`;
        let data = await query(sql);
        let result = {};
        if (data.length) { // '0' 真  0 假
            //查找到了，不给注册
            result = {
                type: 0,
                msg: '该用户存在，不能注册'
            }
        } else {
            //可以注册
            result = {
                type: 1,
                msg: '该用户不存在，可以注册'
            }
        }
        res.send(result); //无论失败与成功都需要响应给客户端
    } else {
        res.send('数据不能为空');

    }

});
// 查找用户最新的电话号码
Router.get('/checkphone', async (req, res) => {
    //接受前端数据
    let {
        username
    } = req.query;
    // console.log(username);
    //做数据库查询
    if (username) {
        let sql = `SELECT * FROM userlist WHERE username='${username}'`;
        let data = await query(sql);
        // console.log(data);
        let result = {};
        if (data.length) { // '0' 真  0 假
            //查找到了，
            result = {
                type: 1,
                msg: '查找成功',
                phone: data[0].phone
            }
        } else {
            //查找不了
            result = {
                type: 0,
                msg: '查找不到手机号'
            }
        }
        res.send(result); //无论失败与成功都需要响应给客户端
    } else {
        res.send('数据不能为空');

    }

});
// 修改用户的手机
Router.put('/editphone', urlencodeParser, express.json(), async (req, res) => {
    //接受前端数据
    let {
        username, phone
    } = req.body;
    // console.log(username, phone);
    //做数据库查询
    if (username && phone) {
        let sql = `UPDATE userlist SET phone=${phone} WHERE username='${username}'`;
        let data = await query(sql);
        let result = {};
        if (data.affectedRows) {
            result = {
                type: 1,
                msg: "修改成功"
            }
        } else {
            //插入数据库失败
            result = {
                type: 0,
                msg: "修改失败"
            }
        }
        res.send(result); //无论失败与成功都需要响应给客户端
    } else {
        res.send('数据不能为空');

    }

});
// 修改用户的密码
Router.put('/editpassword', urlencodeParser, express.json(), async (req, res) => {
    //接受前端数据
    let {
        username, password
    } = req.body;
    // console.log(username, password);
    //做数据库查询
    if (username && password) {
        let sql = `UPDATE userlist SET password='${password}' WHERE username='${username}'`;
        let data = await query(sql);
        let result = {};
        if (data.affectedRows) {
            result = {
                type: 1,
                msg: "修改成功"
            }
        } else {
            //插入数据库失败
            result = {
                type: 0,
                msg: "修改失败"
            }
        }
        res.send(result); //无论失败与成功都需要响应给客户端
    } else {
        res.send('数据不能为空');

    }

});

//注册功能
Router.post('/reg', urlencodeParser, express.json(), async (req, res) => {
    let {
        username,
        password,
        phone
    } = req.body; //解构
    // console.log(req.body);

    if (username && password && phone) {
        let sql = `INSERT INTO userlist(username,password,phone) VALUES('${username}','${password}',${phone})`;
        let data = await query(sql);
        // console.log(data);


        let result = {};
        if (data.affectedRows) {
            //插入成功
            result = {
                type: 1,
                msg: '注册成功'
            }

        } else {
            result = {
                type: 0,
                msg: '注册失败'
            }
        }
        res.send(result);


    } else {
        res.send('请填写全部数据')
    }

})

//需求：查询所有的用户信息
Router.get('/all', async (req, res) => {
    let {
        page,
        num
    } = req.query;
    page = page || 1; //设置默认值
    num = num || 5; //设置默认值
    let index = (page - 1) * num;
    let str = `SELECT * FROM userlist LIMIT ${index},${num}`;
    let data = await query(str);
    // console.log(data);
    let sql2 = 'SELECT * FROM userlist';
    let data2 = await query(sql2);

    let result = {};
    if (data.length) {
        //成功返回的数据
        let pages = Math.ceil(data2.length / num);
        result = {
            type: 1,
            msg: 'success',
            page,
            num,
            pages,
            datalist: data
        }
    } else {
        //失败返回的数据
        result = {
            type: 0,
            msg: 'fail',
            datalist: []
        }
    }

    res.send(result);

})

//需求：删除用户功能
Router.delete("/del", urlencodeParser, async (req, res) => {
    let {
        userid
    } = req.body;
    if (userid) {
        let sql = `DELETE FROM userlist WHERE userid=${userid}`;

        let data = await query(sql);

        let result = {};

        if (data.affectedRows) {
            result = {
                type: 1,
                msg: "删除成功"
            }
        } else {
            //插入数据库失败
            result = {
                type: 0,
                msg: "删除失败"
            }
        }
        res.send(result);
    } else {
        res.send("请填写全部数据");
    }
})

//修改用户
Router.patch('/patch', urlencodeParser, async (req, res) => {
    let = {
        userid,
        password,
        phone
    } = req.body;

    if (userid) {
        let sql = `UPDATE userlist SET password= ${password}, phone=${phone} WHERE userid=${userid}`;

        let data = await query(sql);

        let result = {};
        if (data.affectedRows) {
            result = {
                type: 1,
                msg: "修改成功"
            }
        } else {
            //插入数据库失败
            result = {
                type: 0,
                msg: "修改失败"
            }
        }
        res.send(result);

    } else {
        res.send("请填写全部数据");
    }



})


//需求：登陆功能
Router.get('/login', async (req, res) => {
    //接收前端数据
    // console.log(req);
    let { username, password, keep } = req.query;


    if (username && password) {
        let sql = `SELECT * FROM userlist WHERE username = '${username}' and password='${password}'`;
        let data = await query(sql);
        // console.log(data[0].phone);

        let result = {};
        if (data.length) { // '0' 真  0 假
            //查找到了，可以登陆
            let token = '';
            if (keep == 'true') {// 'true' 'false'
                //需求：7天免登陆，生成token发给你
                //生成token
                token = create(password);
                // console.log(token);
            }
            result = {
                type: 1,
                msg: '登陆成功',
                token,
                phone: data[0].phone
            }
        } else {

            result = {
                type: 0,
                msg: '登陆失败',
                token: ''
            }
        }
        res.send(result);//无论失败与成功都需要响应给客户端
    } else {
        res.send('数据不能为空');
    }

});


//需要：校验token:1.是否被串改  2.是否在有效期
Router.get('/verify', (req, res) => {
    let { token } = req.query;

    let result = verify(token);
    let data = {};
    if (result) {
        data = {
            type: 1,
            msg: '校验通过'
        }
    } else {
        data = {
            type: 0,
            msg: '校验失败'
        }
    }
    res.send(data);
});
module.exports = Router;